package com.chartHandle.excelService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.chartHandle.config.*;
import com.chartHandle.excelService.handle.ExportAllFileCellColorHandler;
import com.chartHandle.excelService.handle.ExportAllFileCellColorHandler2;
import com.chartHandle.excelService.service.WriteSmokeInfoListService;
import com.chartHandle.util.ExcelUtil;
import com.chartHandle.bean.CellStyleWY;
import com.projectCommon.bean.Smoke;
import com.projectCommon.util.springUtil.annotation.MethodEventDescription;
import com.projectCommon.bean.dto.SmokeDto;
import com.chartHandle.bean.PointXY;
import com.projectCommon.util.commonUtil.CUtil;
import com.projectCommon.util.commonUtil.DateUtil;
import com.projectCommon.util.commonUtil.FileUtil;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 得到当前所有卷烟信息的清单
@Service
public class WriteSmokeInfoListServiceImpl implements WriteSmokeInfoListService {
    @Autowired
    private SmokeListTemplate1Config smokeListTemplate1Config;
    @Autowired
    private SmokeListTemplate2Config smokeListTemplate2Config;
    @Autowired
    private SmokeListTemplate2_1Config smokeListTemplate2_1Config;
    @Autowired
    private SmokeOrderTemplate1Config smokeOrderTemplate1Config;
    @Autowired
    private ConfigRead configRead;
    @Override
    public byte[] writeSmokeListInfo(String templateName, List<SmokeDto> smokeDtoList) {
        String listTemplate1Name = FileUtil.getFileNameExcludeSuffix(smokeListTemplate1Config.getFileName());

        String listTemplate2Name = FileUtil.getFileNameExcludeSuffix(smokeListTemplate2Config.getFileName());

        String listTemplate2_1Name = FileUtil.getFileNameExcludeSuffix(smokeListTemplate2_1Config.getFileName());

        byte[] bytes;
        if (templateName.equals(listTemplate2Name) || templateName.equals(listTemplate2_1Name)) {
            bytes = writeListWithListTemplate2(smokeDtoList, templateName);
        } else if (templateName.equals(listTemplate1Name)) {
            bytes = writeListWithListTemplate1(smokeDtoList, templateName);
        } else {
            System.out.println("没有找到对应模板: " + templateName);
            return configRead.getTempNullJpg();
        }
//        return new byte[0];
        return bytes;
    }

    /**
     * 传入查询到的数据，填到 excel 返回字节byte
     * 两种模板填写数据的方式不一样  需要的数据是两套数据  所以就会有两个方法 进行填充
     */
    @MethodEventDescription(event = "use {{#2}} template generate excel", isFillMethodArg = true)
    public byte[] writeListWithListTemplate2(List<SmokeDto> smokeInfos, String templateFileName) {
//        判断是模板2还是模板2_1  根据不同的模板 赋值
        ByteArrayInputStream listTemplateByteArrayInputStream;
        int listTemplateShowColumnWidth;
        if (templateFileName.contains("2_1")) {
            listTemplateByteArrayInputStream = new ByteArrayInputStream(
                    smokeListTemplate2_1Config.getTemplateFileByte());
            listTemplateShowColumnWidth = Integer.parseInt(
                    smokeListTemplate2_1Config.getShowColumnWidth());
        } else {
            listTemplateByteArrayInputStream = new ByteArrayInputStream(
                    smokeListTemplate2Config.getTemplateFileByte());
            listTemplateShowColumnWidth = Integer.parseInt(
                    smokeListTemplate2Config.getShowColumnWidth());
        }

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        List<SmokeDto> generalSmokes = new ArrayList<>();
        List<SmokeDto> abnormitySmokes = new ArrayList<>();
        for (int i = 0; i < smokeInfos.size(); i++) {
            SmokeDto smoke = smokeInfos.get(i);
            if (smoke.getOrgIsAbnormity().equals("是")) {
                int size = abnormitySmokes.size();
                smoke.setId(size + 1 + "");
                smoke.setPicFile(smoke.getPicFile());
                abnormitySmokes.add(smoke);
            } else {
                int size = generalSmokes.size();
                smoke.setId(size + 1 + "");
                smoke.setPicFile(smoke.getPicFile());
                generalSmokes.add(smoke);
            }
        }
        int generalSize = generalSmokes.size();

        // 为常规的烟部分添加颜色 集合
        Map<PointXY, CellStyleWY> pointXYCellStyleWYHashMap = new HashMap<>();
        for (int i = 0; i < generalSmokes.size(); i++) {
            for (int i1 = 0; i1 < listTemplateShowColumnWidth; i1++) {
                CellStyleWY cellStyleWY = new CellStyleWY();
                ExcelUtil.fillCellColor(cellStyleWY, i, smokeListTemplate1Config.getLineColorS());
                pointXYCellStyleWYHashMap.put(new PointXY(i1, i + 2), cellStyleWY);
            }
        }

        HashMap<Integer, List<Integer>> fillColorMap = new HashMap<>();
        for (int i = 0; i < abnormitySmokes.size(); i++) {
            Smoke smoke = abnormitySmokes.get(i);
            if (smoke.getOrgIsCigar().equals("是")) {
                fillColorMap.put(i + 3 + generalSize, CUtil.getNumberList(listTemplateShowColumnWidth));
            }
        }
        ExportAllFileCellColorHandler exportAllFileCellColorHandler = new ExportAllFileCellColorHandler(pointXYCellStyleWYHashMap);
        exportAllFileCellColorHandler.setRows(Integer.parseInt(smokeListTemplate2Config.getFirstHeadHeight()));

        ExportAllFileCellColorHandler2 exportAllFileCellColorHandler2 = new ExportAllFileCellColorHandler2(fillColorMap, IndexedColors.LIGHT_TURQUOISE1.getIndex());
        exportAllFileCellColorHandler2.setRows(Integer.parseInt(
                smokeListTemplate2Config.getSecondHeadHeight()) + Integer.parseInt(
                smokeListTemplate2Config.getFirstHeadHeight()) + generalSize);
        ExcelWriter excelWriterBuild = EasyExcel
                .write(outputStream)
                .withTemplate(listTemplateByteArrayInputStream)
                .excelType(ExcelTypeEnum.XLS)
                .registerWriteHandler(exportAllFileCellColorHandler)
                .registerWriteHandler(exportAllFileCellColorHandler2)
                .build();
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        WriteSheet build = EasyExcel.writerSheet().build();
        excelWriterBuild.fill(new FillWrapper("general", generalSmokes), fillConfig, build);
        excelWriterBuild.fill(new FillWrapper("abnormity", abnormitySmokes), fillConfig, build);
        excelWriterBuild.fill(getSmokeListMap(), build);
        excelWriterBuild.finish();
        return outputStream.toByteArray();
    }
    @MethodEventDescription(event = "use {{#2}} template generate excel", isFillMethodArg = true)
    public byte[] writeListWithListTemplate1(List<SmokeDto> smokeInfos, String templateType) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        for (int i = 0; i < smokeInfos.size(); i++) {
            smokeInfos.get(i).setId(i + 1 + "");
        }

        Map<PointXY, CellStyleWY> pointXYCellStyleWYHashMap = new HashMap<>();
        for (int i = 0; i < smokeInfos.size(); i++) {
            for (int i1 = 0; i1 < Integer.parseInt(
                    smokeListTemplate1Config.getShowColumnWidth()); i1++) {
                CellStyleWY cellStyleWY = new CellStyleWY();
                ExcelUtil.fillCellColor(cellStyleWY, i, smokeOrderTemplate1Config.getLineColorS());
                pointXYCellStyleWYHashMap.put(new PointXY(i1, i + 2), cellStyleWY);
            }
        }

        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        ExcelWriter excelWriterBuild = EasyExcel
                .write(outputStream)
                .withTemplate(new ByteArrayInputStream(smokeListTemplate1Config.getTemplateFileByte()))
                .excelType(ExcelTypeEnum.XLS)
                .registerWriteHandler(new ExportAllFileCellColorHandler(pointXYCellStyleWYHashMap))
                .build();
        excelWriterBuild.fill(smokeInfos, FillConfig.builder().build(), writeSheet);
        excelWriterBuild.fill(getSmokeListMap(), writeSheet);
        excelWriterBuild.finish();
        return outputStream.toByteArray();
    }

    /**
     * 得到 smokelist 的map数据
     *
     * @return
     */
    public Map<String, String> getSmokeListMap() {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("nowTime", DateUtil.queryCurrentDay());
        return stringStringHashMap;
    }

}
